package com.homedo.odin.solution.controller.pc;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.odin.solution.annotation.UserActionLogRecorder;
import com.homedo.odin.solution.enumcode.advertising.CodeTypeEnum;
import com.homedo.odin.solution.enumcode.basic.UserLevelEnum;
import com.homedo.odin.solution.enumcode.statistics.UserActionBusinessTypeEnum;
import com.homedo.odin.solution.model.response.pc.AdServingsResponseBean;
import com.homedo.odin.solution.service.AdServingServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: zhanglei
 * @date: 2018/5/15
 * @Description:
 */
@Api(description = "广告", tags = {"AdServings"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/ad_servings")
public class AdServingsController extends BaseController<AdServingServiceImpl> {


    @ApiOperation(value = "PC_获取页面所有广告", response = AdServingsResponseBean.class, httpMethod = "GET")
    @UserActionLogRecorder(bizType = UserActionBusinessTypeEnum.ADVERTISING_SHOW, name = "title")
    @GetMapping("/page/{pageCode}")
    public ResultData pageAll(@ApiParam(value = "页面code", required = true) @PathVariable String pageCode) {
        List<AdServingsResponseBean> adServings = baseService.getAdServings(pageCode, CodeTypeEnum.PAGE.getDbValue());
        return ResultData.newInstance(Meta.CODE_200, adServings);
    }


    @ApiOperation(value = "PC_获取区域所有广告", response = AdServingsResponseBean.class, httpMethod = "GET")
    @UserActionLogRecorder(bizType = UserActionBusinessTypeEnum.ADVERTISING_SHOW, name = "title")
    @GetMapping("/area/{areaCode}")
    public ResultData getAreaAllAdServings(
            @ApiParam(value = "区域code,方案管广告区域：solution_down", required = true) @PathVariable String areaCode) {
        List<AdServingsResponseBean> adServings = baseService.getAdServings(areaCode, CodeTypeEnum.AREA.getDbValue());
        return ResultData.newInstance(Meta.CODE_200, adServings);
    }

    @ApiOperation(value = "PC_获取指定广告位广告", response = AdServingsResponseBean.class, httpMethod = "GET")
    @UserActionLogRecorder(bizType = UserActionBusinessTypeEnum.ADVERTISING_SHOW, name = "title")
    @GetMapping("/position/{positionCode}")
    public ResultData getPositionAdServing(
            @ApiParam(value = "广告位code,方案管广告区域：solution_down_1 至 solution_down_7  我的财富：my_treasure_top_1", required = true) @PathVariable String positionCode) {
        List<AdServingsResponseBean> adServings = baseService
                .getAdServings(positionCode, CodeTypeEnum.POSITION.getDbValue());
        return ResultData.newInstance(Meta.CODE_200, adServings);
    }

    @ApiOperation(value = "PC_根据用户等级获取指定广告位广告", response = AdServingsResponseBean.class, httpMethod = "GET")
    @UserActionLogRecorder(bizType = UserActionBusinessTypeEnum.ADVERTISING_SHOW, name = "title")
    @GetMapping("/position/{positionCode}/{userLevel}")
    public ResultData getPositionAdServingByUserId(
            @ApiParam(value = "广告位code ， 方案管广告区域：solution_down_1 至 solution_down_7  我的财富：my_treasure_top_1", required = true) @PathVariable String positionCode,
            @ApiParam(value = "用户等级  0-6 获取对应的 全部 新人 铜牌 银牌 金牌 铂金 钻石等对应的广告", required = true) @PathVariable Integer userLevel) {
        if (!UserLevelEnum.dbValueExist(userLevel)) {
            throw new BizException(Meta.CODE_451.getCode(), "用户等级非法");
        }
        List<AdServingsResponseBean> adServings = baseService
                .getAdServingsByUserLevel(positionCode, CodeTypeEnum.POSITION.getDbValue(), userLevel);
        return ResultData.newInstance(Meta.CODE_200, adServings);
    }
}
